package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseDescription;
import com.atguigu.guli.service.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.query.CourseInfoSearch;
import com.atguigu.guli.service.edu.entity.vo.CourseInfoVo;
import com.atguigu.guli.service.edu.service.CourseDescriptionService;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-07-27
 */
@RestController

@RequestMapping("/admin/edu/course")
public class AdminCourseController {

    @Autowired
    CourseService courseService;
    @Autowired
    CourseDescriptionService courseDescriptionService;
    //6.根据id更新课程的发布时间+状态
    @PutMapping("publish/{courseId}")
    public R publish(@PathVariable String courseId){

        LambdaUpdateWrapper<Course> updateWrapper = new LambdaUpdateWrapper<>();
        //Course::getId mp会根据getId  解析id属性上的TableFiled注解，获取该字段在数据库中的列名
        updateWrapper.eq(Course::getId,courseId);
        updateWrapper.set(Course::getStatus,"Normal");
        updateWrapper.set(Course::getPublishTime,new Date());
        courseService.update(updateWrapper);

        return R.ok();


    }

    //5.根据课程id查询，要发布的课程详情
    @GetMapping("get-coursepublish/{courseId}")
    public R getCoursePublish(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCoursePublish(courseId);
        return R.ok().data("item",courseInfoVo);
    }



    //4、带条件的分页查询
    @GetMapping("list/{pageNum}/{pageSize}")
    public R listPage(CourseInfoSearch courseInfoSearch,@PathVariable Integer pageNum,
                      @PathVariable Integer pageSize){
        Page<CourseInfoVo> page = new Page<>(pageNum, pageSize);
        courseService.listPage(page,courseInfoSearch);
        return R.ok().data("page",page);

    }
    //3、更新课程基本信息
    @PutMapping("update-courseinfo/{courseId}")
    public R updateCourseInfo(@RequestBody CourseInfoForm courseInfoForm,@PathVariable String courseId){
        courseService.updateCourseInfo(courseInfoForm,courseId);
        return R.ok();
    }
    //2、查询课程的基本信息回显
    @GetMapping("get-courseinfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        Course course = courseService.getById(courseId);
        CourseDescription description = courseDescriptionService.getById(courseId);
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(course,courseInfoForm);
        courseInfoForm.setDescription(description.getDescription());
        return R.ok().data("item",courseInfoForm);
    }

    //1、处理课程发布第一步请求：保存课程基本信息并返回新增课程的id
    //第二步数据保存时需要使用第一步的课程id
    @PostMapping("save-courseinfo")
    public R saveCourseInfo(@RequestBody CourseInfoForm courseInfoForm){//因为前端提交的数据没有现有的bean可以完全接受，所以需要自定义bean

        String courseId = courseService.saveCourseInfo(courseInfoForm);
        return R.ok().data("id",courseId);
    }
}

